package com.zy.模式匹配.匹配对象

object Test {

  def main(args: Array[String]): Unit = {

    val student: Student = new Student("张三", 12)

    // 对对象的内容进行匹配
    val str = student match {
      case Student("张三", 12) => "匹配成功"
      case _ => "匹配失败"
    }
    println(str)
  }
}

class Student(var name: String, var age: Int)

// 必须定义伴生对象才能进行对象内容匹配
object Student {

  def apply(name: String, age: Int): Student = new Student(name, age)

  // 必须事先unapply方法，用来对对象属性进行拆解
  def unapply(student: Student): Option[(String, Int)] = {
    if (student == null) None
    else Some((student.name, student.age))
  }
}